#!/bin/bash

# Net4Me 部署脚本
# 用于在Linux系统上部署Net4Me服务

set -e  # 遇到错误时停止执行

echo "Net4Me 部署脚本"
echo "================"

# 检查是否以root权限运行
if [ "$EUID" -ne 0 ]; then
    echo "请以root权限运行此脚本:"
    echo "sudo $0"
    exit 1
fi

# 创建net4me用户
echo "创建net4me用户..."
useradd -r -s /bin/false net4me || echo "用户net4me已存在"

# 创建安装目录
echo "创建安装目录..."
mkdir -p /opt/net4me

# 复制文件到安装目录
echo "复制文件到安装目录..."
cp -r ./* /opt/net4me/

# 设置文件权限
echo "设置文件权限..."
chown -R net4me:net4me /opt/net4me
chmod +x /opt/net4me/main.py
chmod +x /opt/net4me/install.sh

# 复制systemd服务文件
echo "复制systemd服务文件..."
cp /opt/net4me/net4me.service /etc/systemd/system/

# 重新加载systemd配置
echo "重新加载systemd配置..."
systemctl daemon-reload

# 启用服务
echo "启用Net4Me服务..."
systemctl enable net4me.service

echo ""
echo "部署完成！"
echo ""
echo "下一步操作："
echo "1. 编辑配置文件 /opt/net4me/config.json"
echo "2. 启动服务: sudo systemctl start net4me"
echo "3. 查看服务状态: sudo systemctl status net4me"
echo "4. 查看日志: sudo journalctl -u net4me -f"